package cn.estore.dao;
//数据库会员表操作类，负责对该表的存取操作

import cn.estore.entity.CustomerEntity;
import cn.estore.util.DBConnection;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

//对会员表的操作
public class CustomerDao {
    private Connection connection = null;
    private PreparedStatement ps = null;
    private DBConnection jdbc = null;

    public CustomerDao() {
        jdbc = new DBConnection();
        connection = jdbc.connection;//利用构造方法取得数据库连接
    }

    public CustomerEntity selectCustomerEntity(String name) {
        CustomerEntity user = null;
        try {
            ps = connection.prepareStatement("select * from tb_customer where user_name=?");
            ps.setString(1, name);//设置SQL语句的查询参数
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                user = new CustomerEntity();
                user.setId(Integer.valueOf(rs.getString(1)));
                user.setUserName(rs.getString(2));
                user.setPassword(rs.getString(3));
                user.setRealName(rs.getString(4));
                user.setMobile(rs.getString(5));

                user.setEmail(rs.getString(6));
                user.setPasswordQuestion(rs.getString(7));
                user.setPasswordHintAnswer(rs.getString(8));

            }
        } catch (SQLException ex) {
            System.out.println("数据库访问失败");
        }
        return user;
    }

    //添加会员注册信息
    public boolean insertCustomer(CustomerEntity user) {
        try {
            ps = connection.prepareStatement("insert into tb_customer(user_name,password,real_name,mobile,email,password_question,password_hint_answer)value (?,?,?,?,?,?,?)");
            ps.setString(1, user.getUserName());
            ps.setString(2, user.getPassword());
            ps.setString(3, user.getRealName());
            ps.setString(4, user.getMobile());
            ps.setString(5, user.getEmail());
            ps.setString(6, user.getPasswordQuestion());
            ps.setString(7, user.getPasswordHintAnswer());
            ps.executeUpdate();
            ps.close();
            return true;
        } catch (SQLException ex) {
            return false;
        }
    }


    //找回密码
    public CustomerEntity selectFind(String name, String result) {
        CustomerEntity user = null;//声明实体里对象
        try {
            ps = connection.prepareStatement("select * from tb_customer where user_name =? and password_hint_answer =?");
            ps.setString(1, name);
            ps.setString(2, result);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                user = new CustomerEntity();
                user.setId(Integer.valueOf(rs.getString(1)));
                user.setUserName(rs.getString(2));
                user.setPassword(rs.getString(3));
                user.setRealName(rs.getString(4));
                user.setMobile(rs.getString(5));

                user.setEmail(rs.getString(6));
                user.setPasswordQuestion(rs.getString(7));
                user.setPasswordHintAnswer(rs.getString(8));

            }
        } catch (SQLException ex) {
            ex.printStackTrace();
            System.out.println("数据库访问失败");
        }
        return user;
    }

    //以数据库自动更新的编号id和密码为参数修改会员密码
    public boolean updatePassword(String password, Integer id) {
        try {
            ps = connection.prepareStatement("update tb_customer set password=? where id=?");
            ps.setString(1, password);
            ps.setInt(2, id.intValue());
            int i = ps.executeUpdate();
            System.out.println(i);
            ps.close();
            return true;
        } catch (SQLException ex) {
            ex.printStackTrace();
            System.out.println(ex);
            return false;
        }
    }



}


